package com.lz.sports.adapter

import com.lz.library.adapter.BindingAdapter
import com.lz.sports.BR
import com.lz.sports.R
import com.lz.sports.bean.DeviceGroup
import com.lz.sports.bean.DeviceInfo

//设备分组
class DeviceGroupAdapter : BindingAdapter<DeviceGroup>() {

    override fun getLayoutResId(viewType: Int): Int = R.layout.item_device_group

    override fun getVariableId(viewType: Int): Int = BR.group

    fun update(groupId: Int) {
        for (i in data.indices) {//id排序? 可二分查找
            if (data[i].id == groupId) {
                data[i].clear() //Fragment意外回收时,此时的Bean对象不一致, 因此还是要同步
                notifyItemChanged(i)
                break
            }
        }
    }

    fun bindDeviceInfo(deviceInfo: DeviceInfo) {
        for (i in data.indices) {
            if (data[i].id == deviceInfo.groupId) {
                data[i].bindDeviceInfo(deviceInfo)
                notifyItemChanged(i)
                break
            }
        }
    }

    fun updateGroupName(deviceGroup: DeviceGroup) {
        for (i in data.indices) {//id排序? 可二分查找
            if (data[i].id == deviceGroup.id) {
                data[i].name = deviceGroup.name
                notifyItemChanged(i)
                break
            }
        }
    }

    fun unbindDeviceInfo(deviceInfo: DeviceInfo) {
        for (i in data.indices) {
            if (data[i].id == deviceInfo.groupId) {
                //Fragment意外回收时,此时的Bean对象不一致, 因此还是要同步
                data[i].unbindDeviceInfo(deviceInfo)
                notifyItemChanged(i)
                break
            }
        }
    }
}